﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmBangLuong : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private ThangTinhLuongBOCollection thangTinhLuongBOCollection = null;
        private BangLuongBOCollection bangLuongBOCollection = null;
        private ThayDoiChucVuReduceBOCollection thayDoiChucVuBOCollection = null;
        private ThayDoiLuongReduceBOCollection thayDoiLuongBOCollection = null;
        private KhoanPhuCapReduceBOCollection phuCapBOCollection = null;
        private TamUngLuongReduceBOCollection tamUngBOCollection = null;
        private PhongBanBOList phongBanBOList = null;
        private ChucVuBOList chucVuBOList = null;

        private string COLUMN_FIRST = "ThuNhapKhac";
        private string COLUMN_SECOND = "Khac";
        #endregion variant

        //=====================================
        #region form
        public FrmBangLuong()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmBangLuong_Load);
            this.Shown += new EventHandler(FrmBangLuong_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmBangLuong_FormClosing);

            this.KeyDown += new KeyEventHandler(FrmBangLuong_KeyDown);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.btnAdd.Click += new EventHandler(btnAdd_Click);
            this.btnLock.Click += new EventHandler(btnLock_Click);
            this.btnUnlock.Click += new EventHandler(btnUnlock_Click);
            this.btnCalc.Click += new EventHandler(btnCalc_Click);

            this.btnPrint.ArrowButtonClick += new EventHandler(btnPrint_ArrowButtonClick);
            this.btnPrint.Click += new EventHandler(btnInTongHopLuong_Click);
            this.btnInPhieuLuong.Click += new EventHandler(btnInPhieuLuong_Click);
            this.btnInPhieuLuong_TatCa.Click += new EventHandler(btnInPhieuLuong_TatCa_Click);
            this.btnInTongHopLuong.Click += new EventHandler(btnInTongHopLuong_Click);
            this.btnDong.Click += new EventHandler(btnDong_Click);

            this.grdViewThang.RowCellStyle += new RowCellStyleEventHandler(grdViewThang_RowCellStyle);

            this.grdView.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdView.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(grdView_CustomDrawGroupRow);

            this.btnLock.Visible = (Session.LoginID == -1);
            this.btnUnlock.Visible = (Session.LoginID == -1);
        }

        private void FrmBangLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                        Save();
                    else
                        if (this.btnPrint.Visible && ((e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F7)))
                            InTongHopLuong();
                        else
                            if ((e.KeyCode == Keys.E && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F2))
                            {
                                if (this.btnAdd.Visible)
                                    Add();
                                else
                                    if (this.btnCalc.Visible)
                                        Calc();
                            }
                            else
                                if (this.btnCancel.Visible && ((e.KeyCode == Keys.H && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F12)))
                                    Cancel();
                                else
                                    if (this.btnUnlock.Visible && e.KeyCode == Keys.M && Control.ModifierKeys == Keys.Control)
                                        UnLock();
                                    else
                                        if (this.btnLock.Visible && e.KeyCode == Keys.K && Control.ModifierKeys == Keys.Control)
                                            Lock();
                                        else
                                            if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                                                this.numNam.Focus();
        }

        private void FrmBangLuong_Shown(object sender, EventArgs e)
        {
            this.grdViewThang.Focus();
        }

        private void FrmBangLuong_Load(object sender, EventArgs e)
        {
            this.numNam.Value = DateTime.Now.Year;

            MyStyle.SetGridStyle(this.grdViewThang, false);
            Utility.InitControl(this.repLookUpPhongBanID);

            RefreshData();
            SetFormStatus(this.formStatus);

            this.numNam.EditValueChanged += new EventHandler(numNam_EditValueChanged);
            this.grdViewThang.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewThang_FocusedRowChanged);
            this.grdView.CellValueChanged += new CellValueChangedEventHandler(grdView_CellValueChanged);
            this.grdView.FocusedRowChanged += new FocusedRowChangedEventHandler(grdView_FocusedRowChanged);
        }

        private void FrmBangLuong_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        private void numNam_EditValueChanged(object sender, EventArgs e)
        {
            FilterNam();
        }
        #endregion control

        //=====================================
        #region button
        private void btnInPhieuLuong_Click(object sender, EventArgs e)
        {
            InPhieuLuong();
        }

        private void btnInPhieuLuong_TatCa_Click(object sender, EventArgs e)
        {
            InPhieuLuong_TatCa();
        }

        private void btnInTongHopLuong_Click(object sender, EventArgs e)
        {
            InTongHopLuong();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.Close();
        }

        private void btnPrint_ArrowButtonClick(object sender, EventArgs e)
        {
            this.contextMenuStrip1.Show(this.pnFooter, new Point(this.btnPrint.Location.X, this.btnPrint.Location.Y - 80));
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            Calc();
        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            UnLock();
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            Lock();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdView_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            if (this.formStatus == FormStatus.Edit)
            {
                string fieldName = e.Column.FieldName;
                if (fieldName == "ThuNhapKhac" || fieldName == "Khac" || fieldName == "Tong")
                {
                    double Luong = 0;
                    double PhuCap = 0;
                    double PhuCapBHXH = 0;
                    double ThuNhapKhac = 0;
                    double Tong = 0;
                    double TamUng = 0;
                    double BHXH = 0;
                    double BHYT = 0;
                    double BHTN = 0;
                    double Khac = 0;
                    double ThucNhan = 0;

                    if (fieldName == "ThuNhapKhac")
                    {
                        Luong = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "Luong"), 0);
                        PhuCap = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "PhuCap"), 0);
                        PhuCapBHXH = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "PhuCapBHXH"), 0);
                        ThuNhapKhac = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "ThuNhapKhac"), 0);
                        Tong = Luong + PhuCap + PhuCapBHXH + ThuNhapKhac;
                        this.grdView.SetRowCellValue(e.RowHandle, "Tong", Tong);
                    }
                    else
                        if (fieldName == "Khac" || fieldName == "Tong")
                        {
                            Tong = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "Tong"), 0);
                            TamUng = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "TamUng"), 0);
                            BHXH = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "BHXH"), 0);
                            BHYT = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "BHYT"), 0);
                            BHTN = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "BHTN"), 0);
                            Khac = StaticUtil.GetDouble(this.grdView.GetRowCellValue(e.RowHandle, "Khac"), 0);
                            ThucNhan = Tong - (TamUng + BHXH + BHYT + BHTN + Khac);
                            this.grdView.SetRowCellValue(e.RowHandle, "ThucNhan", ThucNhan);
                        }
                }
            }
        }

        private void grdViewThang_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterThangTinhLuongID();
        }

        private void grdView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            int? NhanVienID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("NhanVienID"));
            if (NhanVienID == null)
            {
                this.btnInPhieuLuong.Text = "In phiếu lương";
                this.btnInPhieuLuong.Enabled = false;
            }
            else
            {
                this.btnInPhieuLuong.Text = "In phiếu lương nhân viên " + this.grdView.GetFocusedRowCellDisplayText("Ho") + " " + this.grdView.GetFocusedRowCellDisplayText("Ten");
                this.btnInPhieuLuong.Enabled = true;
            }
        }

        private void grdView_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
        {
            GridGroupRowInfo info = e.Info as GridGroupRowInfo;

            if (info.Column.FieldName == "PhongBanID")
            {
                int PhongBanID = StaticUtil.GetInt(info.EditValue);
                if (PhongBanID == int.MinValue)
                    info.GroupText = "Chưa phân phòng ban";
                else
                    if (this.phongBanBOList.ContainsKey(PhongBanID) == false)
                        info.GroupText = "Chưa phân phòng ban";
                    else
                        info.GroupText = this.phongBanBOList.Value(PhongBanID);
            }
        }

        private void grdViewThang_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            int ThangTinhLuongID = StaticUtil.GetInt(this.grdViewThang.GetRowCellValue(e.RowHandle, "ThangTinhLuongID"), -1);
            e.Appearance.ForeColor = (ThangTinhLuongID >= 0 ? Color.Blue : Color.DimGray);
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (this.formStatus == FormStatus.Edit)
            {
                switch (e.Column.FieldName)
                {
                    case "Luong":
                        e.Appearance.ForeColor = Color.FromArgb(192, 0, 0);
                        break;
                    case "Tong":
                        e.Appearance.ForeColor = Color.Green;
                        break;
                    case "ThucNhan":
                        e.Appearance.ForeColor = Color.FromArgb(0, 0, 192);
                        break;
                    case "ThuNhapKhac":
                    case "Khac":
                        e.Appearance.ForeColor = Color.FromArgb(192, 64, 0);
                        break;
                }
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            this.chucVuBOList = ChucVuBOList.GetChucVuBOList();

            this.phongBanBOList = PhongBanBOList.GetPhongBanBOList();
            Utility.SetData(this.repLookUpPhongBanID, this.phongBanBOList, this.phongBanBOList.Count);

            this.thayDoiChucVuBOCollection = ThayDoiChucVuReduceBOCollection.GetThayDoiChucVuReduceBOCollection("", "NhanVienID,NgayApDung");
            this.thayDoiLuongBOCollection = ThayDoiLuongReduceBOCollection.GetThayDoiLuongReduceBOCollection("", "NhanVienID,NgayApDung");
            this.phuCapBOCollection = KhoanPhuCapReduceBOCollection.GetKhoanPhuCapReduceBOCollection("", "NhanVienID,NgayApDung");
            this.tamUngBOCollection = TamUngLuongReduceBOCollection.GetTamUngLuongReduceBOCollection("", "NhanVienID,Ngay");

            if (this.bangLuongBOCollection != null)
                this.bangLuongBOCollection.nhanVienBOCollection = NhanVienLuongBOCollection.GetNhanVienLuongBOCollection();

            FilterNam();
        }

        private void FilterNam()
        {
            if (this.formStatus == FormStatus.View)
            {
                int nam = decimal.ToInt32(this.numNam.Value);
                this.thangTinhLuongBOCollection = ThangTinhLuongBOCollection.GetThangTinhLuongBOCollection("Nam=" + nam, "Thang");
                this.bangLuongBOCollection = BangLuongBOCollection.GetBangLuongBOCollection("Nam=" + nam);

                int i = 0;
                int length = this.thangTinhLuongBOCollection.Count;

                Hashtable hstThang = new Hashtable();
                for (i = 0; i < length; i++)
                    hstThang.Add(this.thangTinhLuongBOCollection[i].Thang.Value, i);

                for (i = 1; i <= 12; i++)
                {
                    if (hstThang.ContainsKey(i) == false)
                    {
                        ThangTinhLuongBO thangTinhLuongBO = ThangTinhLuongBOCollection.NewThangTinhLuongBO();
                        thangTinhLuongBO.Thang = i;
                        thangTinhLuongBO.STT0 = i;
                        thangTinhLuongBO.Nam = nam;
                        thangTinhLuongBO.Ten = "Tháng " + i + " - " + nam;
                        this.thangTinhLuongBOCollection.Add(thangTinhLuongBO);
                    }
                    else
                        this.thangTinhLuongBOCollection[(int)hstThang[i]].STT0 = i;
                }

                this.bdSourceThangTinhLuong.DataSource = this.thangTinhLuongBOCollection;
                this.bdSource.DataSource = this.bangLuongBOCollection.Clone();

                FilterThangTinhLuongID();
            }
        }

        private void FilterThangTinhLuongID()
        {
            if (this.formStatus == FormStatus.View)
            {
                int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
                if (ThangTinhLuongID != null)
                {
                    bool Khoa = StaticUtil.GetBool(this.grdViewThang.GetFocusedRowCellValue("Khoa"));
                    this.grdView.ActiveFilterString = "ThangTinhLuongID=" + ThangTinhLuongID.Value.ToString() + " AND DELE=0";

                    bool isNew = (ThangTinhLuongID.Value < 0);
                    this.btnAdd.Visible = isNew;
                    this.btnCalc.Visible = !isNew;
                    this.btnPrint.Visible = !isNew;
                    this.btnLock.Visible = (!isNew && !Khoa && Session.LoginID == -1);
                    this.btnUnlock.Visible = (!isNew && Khoa && Session.LoginID == -1);

                    this.grdView.CheckLoaded();
                    SetStt();

                    this.lblText.Text = "BẢNG LƯƠNG " + this.grdViewThang.GetFocusedRowCellDisplayText("Ten").ToUpper();
                    this.btnInTongHopLuong.Text = "In bảng lương " + this.grdViewThang.GetFocusedRowCellDisplayText("Ten");
                }
                else
                    this.lblText.Text = "BẢNG LƯƠNG THÁNG";
            }
        }

        private void Add()
        {
            int nam = decimal.ToInt32(this.numNam.Value);
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));

            if (ThangTinhLuongID != null)
            {
                Calc();
                FilterThangTinhLuongID();
            }
        }

        private void Calc()
        {
            int nam = decimal.ToInt32(this.numNam.Value);
            int thang = StaticUtil.GetInt(this.grdViewThang.GetFocusedRowCellValue("Thang"), -1);
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));

            if (ThangTinhLuongID != null)
            {
                int iCompare = nam * 10000 + thang * 100 + (int)Session.thietDatBOCollection.GetDoubleValue(LoaiThietDat.NgayTinhLuong);
                int i = 0;
                int j = 0;
                int length = 0;
                int lengthJ = 0;

                this.bdSource.EndEdit();
                //thay cho this.bangLuongBOCollection.Clone() để nhanh hơn
                BangLuongBOCollection tmpBangLuongBOCollection = (BangLuongBOCollection)this.bdSource.DataSource;

                //xóa hết các tháng tính lương cũ để không hiển thị ra grid
                length = tmpBangLuongBOCollection.Count;
                for (i = 0; i < length; i++)
                    if (tmpBangLuongBOCollection[i].ThangTinhLuongID.Value == ThangTinhLuongID.Value)
                        tmpBangLuongBOCollection[i].DELE = true;

                NhanVienLuongBO nhanVienBO = null;
                ThayDoiChucVuReduceBO chucVuBO = null;
                ThayDoiLuongReduceBO luongBO = null;
                KhoanPhuCapReduceBOCollection tmpPhuCapReduceBOCollection = null;
                bool valid = true;
                int TinhTrangCongViec = 0;
                double PhuCap = 0;
                double PhuCapBHXH = 0;
                double Tong = 0;
                double TamUng = 0;
                double BHXH = 0;
                double BHYT = 0;
                double BHTN = 0;

                double PT_BHXH = StaticUtil.GetDouble(Session.thietDatBOCollection.GetDoubleValue(LoaiThietDat.BHXH), 0) / 100.0;
                double PT_BHYT = StaticUtil.GetDouble(Session.thietDatBOCollection.GetDoubleValue(LoaiThietDat.BHYT), 0) / 100.0;
                double PT_BHTN = StaticUtil.GetDouble(Session.thietDatBOCollection.GetDoubleValue(LoaiThietDat.BHTN), 0) / 100.0;
                double LuongBHXH = 0;

                length = this.bangLuongBOCollection.nhanVienBOCollection.Count;
                for (i = 0; i < length; i++)
                {
                    nhanVienBO = this.bangLuongBOCollection.nhanVienBOCollection[i];

                    //nhân viên còn đang làm việc hay đã nghỉ việc
                    valid = true;
                    TinhTrangCongViec = StaticUtil.GetInt(nhanVienBO.TinhTrangCongViecID, 100);
                    if (TinhTrangCongViec > 1)
                    {
                        if (nhanVienBO.NgayKetThucLamViec == null)
                            valid = false;
                        else
                            if (StaticUtil.GetIntFromDT(nhanVienBO.NgayKetThucLamViec) < iCompare)
                                valid = false;
                    }


                    if (valid) //nhân viên có làm việc trong thời gian đó
                    {
                        luongBO = this.thayDoiLuongBOCollection.GetThayDoiLuongReduceBO(nhanVienBO.NhanVienID.Value, thang, nam);
                        if (luongBO != null)
                        {
                            chucVuBO = this.thayDoiChucVuBOCollection.GetThayDoiChucVuReduceBO(nhanVienBO.NhanVienID.Value, thang, nam);
                            if (chucVuBO != null)
                            {
                                BangLuongBO bangLuongBO = BangLuongBOCollection.NewBangLuongBO();
                                bangLuongBO.MaNhanVien = nhanVienBO.MaNhanVien;
                                bangLuongBO.Ho = nhanVienBO.Ho;
                                bangLuongBO.Ten = nhanVienBO.Ten;
                                bangLuongBO.Nu = nhanVienBO.Nu;
                                bangLuongBO.ThangTinhLuongID = ThangTinhLuongID;
                                bangLuongBO.Nam = nam;
                                bangLuongBO.NhanVienID = nhanVienBO.NhanVienID;
                                bangLuongBO.PhongBanID = chucVuBO.PhongBanID;
                                bangLuongBO.ChucVuID = chucVuBO.ChucVuID;
                                bangLuongBO.Luong = luongBO.ThanhTien;

                                tmpPhuCapReduceBOCollection = this.phuCapBOCollection.FilterData(nhanVienBO.NhanVienID.Value, thang, nam);
                                PhuCap = 0;
                                lengthJ = tmpPhuCapReduceBOCollection.Count;
                                for (j = 0; j < lengthJ; j++)
                                    PhuCap += StaticUtil.GetDouble(tmpPhuCapReduceBOCollection[j].SoTien, 0);

                                bangLuongBO.PhuCap = PhuCap;

                                PhuCapBHXH = StaticUtil.GetDouble(nhanVienBO.BHXH_PCCV, 0) + StaticUtil.GetDouble(nhanVienBO.BHXH_TNK, 0) + StaticUtil.GetDouble(nhanVienBO.BHXH_TNN, 0) + StaticUtil.GetDouble(nhanVienBO.BHXH_TNVK, 0);
                                bangLuongBO.PhuCapBHXH = PhuCapBHXH;

                                Tong = StaticUtil.GetDouble(bangLuongBO.Luong, 0) + PhuCap + PhuCapBHXH;
                                bangLuongBO.Tong = Tong;

                                TamUng = this.tamUngBOCollection.GetTamUng(nhanVienBO.NhanVienID.Value, thang, nam);
                                bangLuongBO.TamUng = TamUng;

                                LuongBHXH = StaticUtil.GetDouble(nhanVienBO.LuongBHXH, 0);

                                BHXH = (StaticUtil.GetBool(nhanVienBO.BHXH) ? PT_BHXH * (LuongBHXH + PhuCapBHXH) : 0);
                                bangLuongBO.BHXH = BHXH;

                                BHYT = (StaticUtil.GetBool(nhanVienBO.BHYT) ? PT_BHYT * (LuongBHXH + PhuCapBHXH) : 0);
                                bangLuongBO.BHYT = BHYT;

                                BHTN = (StaticUtil.GetBool(nhanVienBO.BHTN) ? PT_BHTN * (LuongBHXH + PhuCapBHXH) : 0);
                                bangLuongBO.BHTN = BHTN;

                                bangLuongBO.ThucNhan = Tong - (TamUng + BHXH + BHYT + BHTN);

                                tmpBangLuongBOCollection.Add(bangLuongBO);
                            }
                        }
                    }
                }

                this.bdSource.DataSource = tmpBangLuongBOCollection;
                this.grdView.CheckLoaded();
                SetStt();

                this.formStatus = FormStatus.Edit;
                SetFormStatus(this.formStatus);
                Session.CloseIndex++;
            }
        }

        private void Save()
        {
            int nam = decimal.ToInt32(this.numNam.Value);
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
            if (ThangTinhLuongID != null)
            {
                string oldColumn = this.grdView.FocusedColumn.FieldName;
                string newColumn = (oldColumn == this.COLUMN_FIRST ? this.COLUMN_SECOND : this.COLUMN_FIRST);
                this.grdView.FocusedColumn = this.grdView.Columns[newColumn];

                this.bdSource.EndEdit();
                this.bangLuongBOCollection = (BangLuongBOCollection)this.bdSource.DataSource;

                int i = 0;
                int length = this.bangLuongBOCollection.Count;

                ThangTinhLuongBO thangTinhLuongBO = null;
                bool add = (ThangTinhLuongID < 0);
                if (add)
                {
                    //Thêm tháng tính lương này vào DB
                    thangTinhLuongBO = ThangTinhLuongBO.NewRootThangTinhLuongBO();
                    thangTinhLuongBO.Thang = StaticUtil.GetInt(this.grdViewThang.GetFocusedRowCellValue("Thang"), -1);
                    thangTinhLuongBO.Nam = StaticUtil.GetInt(this.grdViewThang.GetFocusedRowCellValue("Nam"), -1);
                    thangTinhLuongBO.Ten = (string)this.grdViewThang.GetFocusedRowCellValue("Ten");
                    thangTinhLuongBO = thangTinhLuongBO.Save();

                    //Set ID mới cho grid
                    this.grdViewThang.SetFocusedRowCellValue("ThangTinhLuongID", thangTinhLuongBO.ThangTinhLuongID);

                    //Set lại tháng tính lương
                    for (i = 0; i < length; i++)
                        if (this.bangLuongBOCollection[i].ThangTinhLuongID.Value == ThangTinhLuongID.Value)
                            this.bangLuongBOCollection[i].ThangTinhLuongID = thangTinhLuongBO.ThangTinhLuongID;
                }

                this.bangLuongBOCollection = this.bangLuongBOCollection.Save();
                this.bdSource.DataSource = this.bangLuongBOCollection.Clone();

                this.grdView.FocusedColumn = this.grdView.Columns[oldColumn];

                this.formStatus = FormStatus.View;
                SetFormStatus(this.formStatus);
                Session.CloseIndex--;

                FilterThangTinhLuongID();
            }
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSource.DataSource = this.bangLuongBOCollection.Clone();
            FilterThangTinhLuongID();
        }

        private void InPhieuLuong()
        {
            Lock();
            int? NhanVienID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("NhanVienID"));
            if (NhanVienID != null)
            {
                int index = this.grdView.GetDataSourceRowIndex(this.grdView.FocusedRowHandle);
                BangLuongBO blBO = this.bangLuongBOCollection[index];

                RBangLuongBO bangLuongBO = RBangLuongBO.NewRootRBangLuongBO();
                bangLuongBO.CopyFromBangLuongBO(blBO);

                Report.rpBangLuong rBangLuong = new TanHungLong.Report.rpBangLuong(this.phongBanBOList, this.chucVuBOList, bangLuongBO);
                rBangLuong.ShowPreviewDialog();
            }
        }

        private void InPhieuLuong_TatCa()
        {
            Lock();
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
            if (ThangTinhLuongID != null)
            {
                int length = this.bangLuongBOCollection.Count;
                RBangLuongBOCollection rBangLuongBOCollection = RBangLuongBOCollection.NewRBangLuongBOCollection();
                for (int i = 0; i < length; i++)
                {
                    if (this.bangLuongBOCollection[i].ThangTinhLuongID == ThangTinhLuongID)
                    {
                        RBangLuongBO bangLuongBO = RBangLuongBOCollection.NewRBangLuongBO();
                        bangLuongBO.CopyFromBangLuongBO(this.bangLuongBOCollection[i]);
                        rBangLuongBOCollection.Add(bangLuongBO);
                    }
                }

                //
                Report.rpDanhSachBangLuong report = new TanHungLong.Report.rpDanhSachBangLuong(this.phongBanBOList, this.chucVuBOList, rBangLuongBOCollection);
                report.ShowPreviewDialog();
            }
        }

        private void InTongHopLuong()
        {
            Lock();
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
            if (ThangTinhLuongID != null)
            {
                double TongTien = 0;
                int length = this.bangLuongBOCollection.Count;
                RBangLuongBOCollection rBangLuongBOCollection = RBangLuongBOCollection.NewRBangLuongBOCollection();
                for (int i = 0; i < length; i++)
                {
                    if (this.bangLuongBOCollection[i].ThangTinhLuongID == ThangTinhLuongID)
                    {
                        RBangLuongBO bangLuongBO = RBangLuongBOCollection.NewRBangLuongBO();
                        bangLuongBO.CopyFromBangLuongBO(this.bangLuongBOCollection[i]);
                        TongTien += StaticUtil.GetDouble(bangLuongBO.ThucNhan, 0);
                        rBangLuongBOCollection.Add(bangLuongBO);
                    }
                }

                Report.rpTongHopBangLuong report = new TanHungLong.Report.rpTongHopBangLuong(this.phongBanBOList, this.chucVuBOList, rBangLuongBOCollection);
                report.ShowPreviewDialog();
            }
        }

        private void Lock()
        {
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
            if (ThangTinhLuongID != null)
                if (ThangTinhLuongID >= 0)
                {
                    ThangTinhLuongBO thangTinhLuongBO = ThangTinhLuongBO.GetRootThangTinhLuongBO(ThangTinhLuongID.Value);
                    if (thangTinhLuongBO != null)
                    {
                        thangTinhLuongBO.Khoa = true;
                        thangTinhLuongBO = thangTinhLuongBO.Save();

                        this.grdViewThang.SetFocusedRowCellValue("Khoa", true);

                        this.btnLock.Visible = false;
                        this.btnUnlock.Visible = (Session.LoginID == -1);
                    }
                }
        }

        private void UnLock()
        {
            int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
            if (ThangTinhLuongID != null)
                if (ThangTinhLuongID >= 0)
                {
                    ThangTinhLuongBO thangTinhLuongBO = ThangTinhLuongBO.GetRootThangTinhLuongBO(ThangTinhLuongID.Value);
                    if (thangTinhLuongBO != null)
                    {
                        thangTinhLuongBO.Khoa = false;
                        thangTinhLuongBO = thangTinhLuongBO.Save();

                        this.grdViewThang.SetFocusedRowCellValue("Khoa", false);

                        this.btnLock.Visible = (Session.LoginID == -1);
                        this.btnUnlock.Visible = false;
                    }
                }
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.grdView, edit);

            this.numNam.Enabled = !edit;
            this.grdControlThang.Enabled = !edit;

            this.btnAdd.Visible = !edit;
            this.btnLock.Visible = !edit;
            this.btnUnlock.Visible = !edit;
            this.btnPrint.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            foreach (GridColumn col in this.grdView.Columns)
            {
                if (col.Visible)
                {
                    if (col.FieldName == "ThuNhapKhac" || col.FieldName == "Khac")
                        col.OptionsColumn.AllowEdit = edit;
                    else
                        col.OptionsColumn.AllowFocus = !edit;
                }
            }

            this.grdView.Focus();

            if (edit)
            {
                string focusColumn = this.grdView.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST;

                this.grdView.FocusedColumn = this.grdView.Columns[focusColumn];
                this.grdView.ShowEditor();
            }
            else
            {
                int? ThangTinhLuongID = StaticUtil.GetRealInt(this.grdViewThang.GetFocusedRowCellValue("ThangTinhLuongID"));
                if (ThangTinhLuongID != null)
                {
                    bool Khoa = StaticUtil.GetBool(this.grdViewThang.GetFocusedRowCellValue("Khoa"));
                    bool isNew = (ThangTinhLuongID.Value < 0);
                    this.btnAdd.Visible = isNew;
                    this.btnCalc.Visible = !isNew;
                    this.btnPrint.Visible = !isNew;
                    this.btnLock.Visible = (!isNew && !Khoa && Session.LoginID == -1);
                    this.btnUnlock.Visible = (!isNew && Khoa && Session.LoginID == -1);
                }
            }
        }

        private void SetStt()
        {
            if (this.formStatus == FormStatus.Edit)
                this.bdSource.EndEdit();

            int stt = 0;
            int length = this.grdView.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.grdView.GetVisibleRowHandle(i);
                if (index >= 0)
                    this.grdView.SetRowCellValue(index, "STT0", ++stt);
            }
        }
        #endregion function
    }
}